package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.enums.AuditStateEnum;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ApprovalMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审批Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-10
 */
@Service
public class ApprovalServiceImpl implements IApprovalService {
    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private IWritingsService writingsService;
    @Autowired
    private IAppraisalService appraisalService;
    @Autowired
    private IContractService contractService;
    @Autowired
    private IPaperService paperService;
    @Autowired
    private IPatentService patentService;
    @Autowired
    private IPrizeService prizeService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISoftwareService softwareService;


    /**
     * 查询审批
     *
     * @param id 审批ID
     * @return 审批
     */
    @Override
    public Approval selectApprovalById(Long id) {
        return approvalMapper.selectApprovalById(id);
    }

    /**
     * 查询审批列表
     *
     * @param approval 审批
     * @param sysUser
     * @return 审批
     */
    @Override
    public List<Approval> selectApprovalList(Approval approval, SysUser sysUser) {
        approval.setApplyManDept(sysUser.getDeptId());
        approval.setApprovalResult(AuditStateEnum.NOT_AUDIT.getValue());
        return approvalMapper.selectApprovalList(approval);
    }

    /**
     * 新增审批
     *
     * @param approval 审批
     * @return 结果
     */
    @Override
    public int insertApproval(Approval approval) {
        return approvalMapper.insertApproval(approval);
    }


    /**
     * 修改审批
     *
     * @param approval 审批
     * @return 结果
     */
    @Override
    public int updateApproval(Approval approval) {
        return approvalMapper.updateApproval(approval);
    }

    /**
     * 删除审批对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApprovalByIds(String ids) {
        return approvalMapper.deleteApprovalByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审批信息
     *
     * @param id 审批ID
     * @return 结果
     */
    @Override
    public int deleteApprovalById(Long id) {
        return approvalMapper.deleteApprovalById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, Integer auditState, String remark, SysUser sysUser) {
        Approval approval = selectApprovalById(id);
        // TODO 审核状态    1  审核通过   2  审核失败
        if (1 == auditState){
            auditState = 3;
        }else{
            auditState = 4;
        }
        approval.setRemark(remark);
        approval.setApprovalResult(auditState);
        approval.setApprovalMan(sysUser.getUserName());
        approval.setApprovalManId(sysUser.getUserId());
        approval.setApprovalTime(new Date());
        updateApproval(approval);
        Long docRelationId = approval.getDocRelationId();
        Integer docType = approval.getDocType();
        DocTypeEnum iEnum = EnumUtil.getIEnum(DocTypeEnum.class, docType);

        switch (iEnum) {
            case PAPER:
                Paper paper = paperService.selectPaperById(docRelationId);
                paper.setAuditState(auditState);
                paperService.updatePaper(paper);
                break;
            case PRIZE:
                Prize prize = prizeService.selectPrizeById(docRelationId);
                prize.setAuditState(auditState);
                prizeService.updatePrize(prize);
                break;
            case PATENT:
                Patent patent = patentService.selectPatentById(docRelationId);
                patent.setAuditState(auditState);
                patentService.updatePatent(patent);
                break;
            case REPORT:
                Report report = reportService.selectReportById(docRelationId);
                report.setAuditState(auditState);
                reportService.updateReport(report);
                break;
            case CONTRACT:
                Contract contract = contractService.selectContractById(docRelationId);
                contract.setAuditState(auditState);
                contractService.updateContract(contract);
                break;
            case SOFTWARE:
                Software software = softwareService.selectSoftwareById(docRelationId);
                software.setAuditState(auditState);
                softwareService.updateSoftware(software);
                break;
            case WRITINGS:
                Writings writings = writingsService.selectWritingsById(docRelationId);
                writings.setAuditState(auditState);
                writingsService.updateWritings(writings);
                break;
            case APPRAISAL:
                Appraisal appraisal = appraisalService.selectTAppraisalById(docRelationId);
                appraisal.setAuditState(auditState);
                appraisalService.updateTAppraisal(appraisal);
                break;
        }
    }
}
